import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'

import NProgress from 'nprogress' //进度条
import 'nprogress/nprogress.css'

NProgress.configure({
  showSpinner: false //进度条去除圆形转圈圈图案
})

const router = createRouter({
  // import.meta是js暴露描述模块的文件信息对象
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: () => import('@/views/login/index.vue'),
      meta: {
        title: '登录'
      }
    },
    {
      path: '/login/callback',
      component: () => import('@/views/login/loginCallback.vue'),
      meta: { title: 'QQ登录-绑定手机' }
    },
    {
      path: '/order/pay',
      component: () => import('@/views/order/orderPay.vue'),
      meta: { title: '药品支付' }
    },
    {
      path: '/order/pay/result',
      component: () => import('@/views/order/orderPayResult.vue'),
      meta: { title: '药品支付结果' }
    },
    {
      path: '/order/:id',
      component: () => import('@/views/order/orderDetail.vue'),
      meta: { title: '药品订单详情' }
    },
    {
      path: '/order/logistics/:id',
      component: () => import('@/views/order/orderLogistics.vue'),
      meta: { title: '物流详情' }
    },
    {
      path: '/user/patient',
      component: () => import('@/views/user/patientPage.vue'),
      meta: {
        title: '家庭档案'
      }
    },
    {
      path: '/user/consult',
      component: () => import('@/views/user/consultPage.vue'),
      meta: { title: '问诊记录' }
    },
    {
      path: '/user/consult/:id',
      component: () => import('@/views/user/consultDetail.vue'),
      meta: { title: '问诊详情' }
    },
    {
      path: '/consult/fast',
      component: () => import('@/views/consult/consultFast.vue'),
      meta: {
        title: '极速问诊'
      }
    },
    {
      path: '/consult/dep',
      component: () => import('@/views/consult/consultDep.vue'),
      meta: {
        title: '选择科室'
      }
    },
    {
      path: '/consult/illness',
      component: () => import('@/views/consult/consultIllness.vue'),
      meta: {
        title: '病情描述'
      }
    },
    {
      path: '/consult/pay',
      component: () => import('@/views/consult/consultPay.vue'),
      meta: {
        title: '问诊支付'
      }
    },
    {
      path: '/consult/medicine',
      component: () => import('@/views/consult/consultMedicine.vue'),
      meta: { title: '开药门诊' }
    },
    {
      path: '/consult/choose',
      component: () => import('@/views/consult/consultChoose.vue'),
      meta: { title: '选择药品' }
    },
    {
      path: '/medicineDetail/:id',
      component: () => import('@/views/consult/consultMedicineDetail.vue'),
      meta: { title: '药品详情' }
    },
    {
      path: '/consult/doctor',
      component: () => import('@/views/consult/consultDoctor.vue'),
      meta: { title: '找医生' }
    },
    {
      path: '/doctorList/:depId',
      component: () => import('@/views/consult/consultDoctorList.vue'),
      meta: { title: '医生搜索' }
    },
    {
      path: '/doctorDetail/:id',
      component: () => import('@/views/consult/consultDoctorDetail.vue'),
      meta: { title: '医生详情' }
    },
    {
      path: '/room',
      component: () => import('@/views/room/index.vue'),
      meta: {
        title: '问诊室'
      },
      beforeEnter: (to) => {
        if (to.query.payResult === 'false') return '/user/consult'
      }
    },
    {
      path: '/',
      redirect: '/home',
      component: () => import('@/views/layout/index.vue'),
      children: [
        {
          path: '/home',
          component: () => import('@/views/home/index.vue'),
          meta: {
            title: '首页'
          }
        },
        {
          path: '/article',
          component: () => import('@/views/article/index.vue'),
          meta: {
            title: '健康百科'
          }
        },
        {
          path: '/notify',
          component: () => import('@/views/notify/index.vue'),
          meta: {
            title: '消息通知'
          }
        },
        {
          path: '/user',
          component: () => import('@/views/user/index.vue'),
          meta: {
            title: '我的'
          }
        }
      ]
    }
  ]
})

export default router

// 全局的前置导航
router.beforeEach((to) => {
  NProgress.start()
  // 无token在白名单放行不在白名单 去登录页
  const store = useUserStore()

  const whiteList = ['/login', '/login/callback']
  if (!store.user?.token && !whiteList.includes(to.path)) return '/login'
})

// 后置守卫
router.afterEach((to) => {
  document.title = `${to.meta.title || ''}- ${import.meta.env.VITE_APP_TITLE}`
  NProgress.done()
})
